package com.cmc6.websocket.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc6.common.model.dto.ChangeByteById;
import com.cmc6.common.model.dto.ChangeByteDTO;
import com.cmc6.common.model.dto.NotEmptyIdSet;
import com.cmc6.common.util.ApiResult;
import com.cmc6.websocket.model.dto.SocketPageDTO;
import com.cmc6.websocket.model.vo.SocketPageVO;
import com.cmc6.websocket.model.vo.SocketRegVO;
import com.cmc6.websocket.service.SocketService;
import com.cmc6.websocket.service.WebSocketHelperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/socket")
@Api(tags = "socket控制器")
public class SocketController {

    @Resource
    SocketService baseService;
    @Resource
    WebSocketHelperService webSocketHelperService;

    @PostMapping("/reg")
    @ApiOperation(value = "获取 socket连接地址和随机码")
    public ApiResult<SocketRegVO> reg(@RequestBody @Valid ChangeByteDTO changeByteDTO) {
        return ApiResult.ok(webSocketHelperService.reg(changeByteDTO));
    }

    @PreAuthorize("hasAuthority('socket:page')")
    @PostMapping("/page")
    @ApiOperation(value = "分页排序查询 socket连接记录")
    public ApiResult<Page<SocketPageVO>> myPage(@RequestBody @Valid SocketPageDTO dto) {
        return ApiResult.ok(webSocketHelperService.myPage(dto));
    }

    @PostMapping("/changeType")
    @ApiOperation(value = "更改在线状态")
    public ApiResult<String> changeType(@RequestBody @Valid ChangeByteById changeByteById) {
        return ApiResult.ok(webSocketHelperService.changeType(changeByteById));
    }

    @PreAuthorize("hasAuthority('socket:insertOrUpdate')")
    @PostMapping("/offlineByIdSet")
    @ApiOperation(value = "强退，通过 idSet")
    public ApiResult<String> offlineByIdSet(@RequestBody @Valid NotEmptyIdSet notEmptyIdSet) {
        return ApiResult.ok(baseService.offlineByIdSet(notEmptyIdSet));
    }

    @PreAuthorize("hasAuthority('socket:insertOrUpdate')")
    @PostMapping("/offlineAll")
    @ApiOperation(value = "全部强退")
    public ApiResult<String> offlineAll() {
        return ApiResult.ok(baseService.offlineAll());
    }

}
